import { _decorator } from 'cc';
import { BaseManager } from '../../libs/rxjs/cc3/BaseManager';
import { color } from 'cc';
import { director } from 'cc';
import { System } from 'cc';
import { sys } from 'cc';
import { jsb_sdk_m } from './JsbSDKManager';
import { ReactiveProperty } from '../../libs/rxjs/cc3/ReactiveProperty';
import { udm } from '../UserDataManager';
const { ccclass, property } = _decorator;

@ccclass('MiscSDKManager')
export class MiscSDKManager extends BaseManager {

    public static instance: MiscSDKManager;
    static ID = 'misc_sdk_system';
    get logColorOverride() { return color().fromHEX('#EECCDD') };

    infoDic = ReactiveProperty.Create<Record<string, any>>(null);
    deviceInfoDic = ReactiveProperty.Create<Record<string, any>>(null);
    idfa = ReactiveProperty.Create<string>(null);
    ata = ReactiveProperty.Create<string>(null);
    isTestFlightBeta = ReactiveProperty.Create<boolean>(null);

    static registerSystem() {
        director.unregisterSystem(director.getSystem(MiscSDKManager.ID));
        director.registerSystem(MiscSDKManager.ID, misc_sdk_m, System.Priority.MEDIUM);
        misc_sdk_m.init();
    }

    onInitManager() {
        this.log('onInitManager', this.timestamp);
        if (sys.isNative) {
            jsb_sdk_m.registerMsg('MiscSDKManager.responseInfoDictionary', this.responseInfoDictionary.bind(this));
            jsb_sdk_m.registerMsg('MiscSDKManager.responseDeviceInfo', this.responseDeviceInfo.bind(this));
            jsb_sdk_m.registerMsg('MiscSDKManager.responseIDFA', this.responseIDFA.bind(this));
            jsb_sdk_m.registerMsg('MiscSDKManager.responseATA', this.responseATA.bind(this));
            jsb_sdk_m.registerMsg('MiscSDKManager.responseIsTestFlightBeta', this.responseIsTestFlightBeta.bind(this));
        }

        this.requestInfoDictionary();
        this.requestDeviceInfo();
        this.requestIsTestFlightBeta();
        // this.requestIDFA();
    }

    useObserves() {
        return [
            this.ata.subscribe(_ => {
                if (this.ata.value == 'authorized') {
                    this.requestIDFA();
                }
            })
        ];
    }

    requestInfoDictionary() {
        if (sys.isNative) {
            jsb_sdk_m.sendMsg('MiscSDK.requestInfoDictionary', '{}');
        }
    }

    responseInfoDictionary(param: string) {
        this.log('@responseInfoDictionary', param);
        this.infoDic.value = JSON.parse(param);
    }

    requestDeviceInfo() {
        if (sys.isNative) {
            jsb_sdk_m.sendMsg('MiscSDK.requestDeviceInfo', '{}');
        }
    }

    responseDeviceInfo(param: string) {
        this.log('@responseDeviceInfo', param);
        this.deviceInfoDic.value = JSON.parse(param);
    }

    requestATA() {
        if (sys.isNative) {
            jsb_sdk_m.sendMsg('MiscSDK.requestATA', '{}');
        }
    }

    responseATA(param: string) {
        this.log('@responseATA', param);
        let { result } = JSON.parse(param);
        this.ata.value = result;
        this.log('@ATA:', result);
    }

    requestIDFA() {
        if (sys.isNative) {
            jsb_sdk_m.sendMsg('MiscSDK.requestIDFA', '{}');
        }
    }

    responseIDFA(param: string) {
        this.log('@responseIDFA', param);
        let { IDFA } = JSON.parse(param);
        this.idfa.value = IDFA;
        this.log('@IDFA:', IDFA);
    }

    requestHapic(style: string) {
        if (!udm.vibrateOn.value) return;
        if (sys.isNative) {
            if (sys.platform == sys.Platform.IOS) {
                jsb_sdk_m.sendMsg('MiscSDK.requestHaptic', JSON.stringify({ style }));
            } else if (sys.platform == sys.Platform.ANDROID) {
                let amp = 'default';
                let duration = 0;
                if (style == 'light') {
                    amp = 'tick';
                    duration = 20;
                } else if (style == 'medium') {
                    amp = 'tick';
                    duration = 25;
                } else if (style == 'soft' || style == 'heavy') {
                    amp = 'tick';
                    duration = 30;
                }
                jsb_sdk_m.sendMsg('MiscSDK.requestHaptic', JSON.stringify({
                    amp, duration
                }));
            }
        }
    }

    getNativeBuild() {
        if (this.infoDic.value != null) {
            if (sys.platform == sys.Platform.IOS) {
                return this.infoDic.value['CFBundleVersion'];
            } else if (sys.platform == sys.Platform.ANDROID) {
                return this.infoDic.value['versionCode'];
            }
        }
        return 0;
    }

    getNativeVersion() {
        if (this.infoDic.value != null) {
            if (sys.platform == sys.Platform.IOS) {
                return this.infoDic.value['CFBundleShortVersionString'];
            } else if (sys.platform == sys.Platform.ANDROID) {
                return this.infoDic.value['versionName'];
            }
        }
        return '0';
    }

    getPhoneModel() {
        if (this.deviceInfoDic.value != null) {
            if (sys.platform == sys.Platform.IOS) {
                return this.deviceInfoDic.value?.['phoneModel'];
            } else if (sys.platform == sys.Platform.ANDROID) {
                return `${this.deviceInfoDic.value?.['brand']} ${this.deviceInfoDic.value?.['model']}`;
            }
        }
    }

    requestIsTestFlightBeta() {
        if (sys.isNative) {
            jsb_sdk_m.sendMsg('MiscSDK.requestIsTestFlightBeta', '{}');
        }
    }

    responseIsTestFlightBeta(param: string) {
        this.log('@responseIsTestFlightBeta', param);
        let { result } = JSON.parse(param);
        this.isTestFlightBeta.value = result;
    }
}

globalThis.MiscSDKManager = MiscSDKManager;

export const misc_sdk_m = MiscSDKManager.instance = new MiscSDKManager();